<template>
  <el-dialog 
    :visible.sync="visible" 
    :title="!dataForm.userId ? '添加':'修改'"
    :close-on-click-modal="false" 
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  @keydown.enter.native.prevent="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="noticeTopic" label="主题">
        <el-input v-model="dataForm.noticeTopic" placeholder="主题"></el-input>
      </el-form-item>
      <el-form-item prop="target" label="发送对象">
        <el-select v-model="dataForm.target" placeholder="请选择">
          <el-option label="全体用户" value=0></el-option>
          <el-option label="学生" value=1></el-option>
          <el-option label="教师" value=2></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      </el-form-item> -->
      <el-form-item prop="noticeContent" label="公告内容">
        <!-- 公告内容 -->
        <mavon-editor ref="md" v-model="dataForm.noticeContent" style="height:calc(100vh - 260px)" />

        <!-- <el-input type="textarea" v-model="dataForm.noticeContent" maxlength="1000"
  show-word-limit placeholder="请输入公告内容"></el-input> -->
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">发布</el-button>
    </template>
  </el-dialog>
</template>

<script>
import servicesApi from '@/services/servicesApi'
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        userId: '',
        noticeTopic: '',
        target: '',
        noticeContent: '',
        target: '',
      }
    }
  },
  computed: {
    dataRule() {
      return {
        noticeTopic: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        target: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        noticeContent: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(userId) {
      this.dataForm.userId =userId || 0
      // console.log('userId=%s', userId)
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.userId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      // servicesApi.getDictInfoById(this.dataForm.userId).then(res=>{
      //   if (res.data.code !== 200) {
      //     return this.$message.error(res.data.msg)
      //   }
      //   this.dataForm = {
      //     ...this.dataForm,
      //     ...res.data.data
      //   }
      // })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.dataForm.userId) { // 修改
          // console.log('修改公告')
          servicesApi.updateDict(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        } else {  // 新增
          // console.log('新增公告')
          servicesApi.addDict(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '发布成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
          })
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
